using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class SinhVienRepository
	{
		public static List<SinhVien> SelectAll()
		{
			try
			{
				return DataContext.Instance.SinhVien.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<SinhVien>();
		}

        public static List<SinhVien> TimKiem(string mssv, string makhoa, string macn, string mahdt, string makh)
        {
            int iMaKhoa, iMaCN, iMaHDT, iMaKH;

            try
            {
                int.TryParse(makhoa, out iMaKhoa);
                int.TryParse(macn, out iMaCN);
                int.TryParse(mahdt, out iMaHDT);
                int.TryParse(makh, out iMaKH);

                return DataContext.Instance.SinhVien.Where
                    (
                        n => n.MSSV == (string.Empty.Equals(mssv) ? n.MSSV : mssv) &&
                        n.ChuyenNganh.Khoa.MaKhoa == (string.Empty.Equals(makhoa) ? n.ChuyenNganh.Khoa.MaKhoa : iMaKhoa) &&
                        n.MaCN.Value == (string.Empty.Equals(macn) ? n.MaCN.Value : iMaCN) &&
                        n.MaHDT.Value == (string.Empty.Equals(mahdt) ? n.MaHDT.Value : iMaHDT) &&
                        n.MaKH.Value == (string.Empty.Equals(makh) ? n.MaKH.Value : iMaKH)
                    ).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return new List<SinhVien>();
        }

		public static SinhVien SelectByID(string mssv)
		{
			try
			{
				return DataContext.Instance.SinhVien.FirstOrDefault(item => item.MSSV == mssv );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(SinhVien obj)
		{
			try
			{
				DataContext.Instance.SinhVien.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(string mssv)
		{
			try
			{
				var delitem = DataContext.Instance.SinhVien.FirstOrDefault(item => item.MSSV == mssv );
				DataContext.Instance.SinhVien.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<SinhVien> RetrieveByID(string mssv)
		{
			try
			{
				return (from item in DataContext.Instance.SinhVien where  item.MSSV == mssv  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<SinhVien>();
		}

		public static List<SinhVien> SelectByMaCN(int macn)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.SinhVien where item.MaCN == macn select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<SinhVien>();
		}

        public static List<SinhVien> SelectByMaKhoa(int makhoa)
        {
            try
            {
                var query = from item in DataContext.Instance.SinhVien
                            where item.ChuyenNganh.MaKhoa == makhoa
                            group item by item.MaCN into g
                            select g.OrderBy(n => n.HoTen);
                var lst = new List<SinhVien>();
                foreach (var groupGV in query)
                {
                    foreach (var gv in groupGV)
                    {
                        lst.Add(gv);
                    }
                }
                return lst;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return new List<SinhVien>();
        }

		public static List<SinhVien> SelectByMaHDT(int mahdt)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.SinhVien where item.MaHDT == mahdt select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<SinhVien>();
		}

		public static List<SinhVien> SelectByMaKH(int makh)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.SinhVien where item.MaKH == makh select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<SinhVien>();
		}

	}

}
